<template>
  <div>
    <h1>拼团活动列表(平台)</h1>
    <div>
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="活动名称">
          <el-input
            v-model="searchForm.keyWord"
            placeholder="请输入活动名称"
            clearable
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="getGroupActive()">查询</el-button>
          <el-button type="success" @click="onAddGroupActive()">添加</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <el-table :data="groupActiveData" border style="width: 100%">
        <el-table-column prop="activeId" label="活动Id" />
        <el-table-column prop="activeName" label="活动名称" />
        <el-table-column prop="groupNum" label="参团人数" />
        <el-table-column prop="beginTime" label="开团时间" />
        <el-table-column prop="endTime" label="结束时间" />
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="small" @click="onUpdateActive(scope.row)">
              修改
            </el-button>
            <el-button
              size="small"
              type="danger"
              @click="ondeleteById(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div>
      <el-pagination
        v-model:current-page="searchForm.pageNum"
        v-model:page-size="searchForm.pageSize"
        :page-sizes="[2, 3, 4, 5]"
        :background="true"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <div>
      <el-dialog v-model="dialogFormVisible" title="添加拼团活动" width="500">
        <el-form :model="form" :rules="rules" ref="ruleFormRef">
          <el-form-item
            label="活动名称"
            prop="activeName"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.activeName" autocomplete="off" />
          </el-form-item>
          <el-form-item
            label="参团人数"
            prop="groupNum"
            :label-width="formLabelWidth"
          >
            <el-input-number v-model="form.groupNum" :min="1" />
          </el-form-item>
          <el-form-item label="开团日期" :label-width="formLabelWidth">
            <el-date-picker
              v-model="form.beginTime"
              type="datetime"
              placeholder="请输入开团时间"
              value-format="YYYY-MM-DD HH:mm:ss"
            />
          </el-form-item>
          <el-form-item label="关团日期" :label-width="formLabelWidth">
            <el-date-picker
              v-model="form.endTime"
              type="datetime"
              placeholder="请输入关团时间"
              value-format="YYYY-MM-DD HH:mm:ss"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button type="primary" @click="submitForm(ruleFormRef)">
              添加
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
    <div>
      <el-dialog
        v-model="dialogUpdateFormVisible"
        title="修改拼团活动"
        width="500"
      >
        <el-form :model="form">
          <el-form-item label="活动名称" :label-width="formLabelWidth">
            <el-input
              v-model="form.activeName"
              disabled="disabled"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item label="参团人数" :label-width="formLabelWidth">
            <el-input-number v-model="form.groupNum" :min="1" />
          </el-form-item>
          <el-form-item label="开团日期" :label-width="formLabelWidth">
            <el-date-picker
              v-model="form.beginTime"
              type="datetime"
              placeholder="请输入开团时间"
              value-format="YYYY-MM-DD HH:mm:ss"
            />
          </el-form-item>
          <el-form-item label="关团日期" :label-width="formLabelWidth">
            <el-date-picker
              v-model="form.endTime"
              type="datetime"
              placeholder="请输入关团时间"
              value-format="YYYY-MM-DD HH:mm:ss"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogUpdateFormVisible = false">取消</el-button>
            <el-button type="primary" @click="onUpdateById()"> 修改 </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import {
  getGroupActiveList,
  updateGroupActive,
  addGroupActive,
  deleteById,
} from "../api/api";
import { ElMessage, ElMessageBox } from "element-plus";
import type { FormInstance, FormRules } from "element-plus";
const ruleFormRef = ref<FormInstance>();
const onUpdateActive = (row: any) => {
  Object.assign(form.value, row);
  dialogUpdateFormVisible.value = true;
};
const onUpdateById = () => {
  updateGroupActive(form.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("修改成功");
      dialogUpdateFormVisible.value = false;
      getGroupActive();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
const ondeleteById = (row: any) => {
  ElMessageBox.confirm("您确定要删除么?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      deleteById(row.activeId).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("删除成功");
          getGroupActive();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    })
    .catch(() => {});
};

const dialogFormVisible = ref(false);
const dialogUpdateFormVisible = ref(false);
const formLabelWidth = "140px";

const form = ref({
  activeName: "",
  beginTime: "",
  endTime: "",
  groupNum: null,
});
const onAddGroupActive = () => {
  form.value = {
    activeName: "",
    beginTime: "",
    endTime: "",
    groupNum: null,
  };
  dialogFormVisible.value = true;
};
const onSubmitAdd = () => {
  addGroupActive(form.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("添加成功");
      dialogFormVisible.value = false;
      getGroupActive();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

const rules = ref<FormRules>({
  activeName: [{ required: true, message: "请输入活动名称", trigger: "blur" }],
  groupNum: [{ required: true, message: "请输入活动数量", trigger: "blur" }],
});

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      onSubmitAdd();
    } else {
      console.log("error submit!", fields);
    }
  });
};

const groupActiveData = ref([]);
const searchForm = ref({
  pageNum: 1,
  pageSize: 2,
  keyWord: null,
});
const total = ref(0);
const getGroupActive = () => {
  getGroupActiveList(searchForm.value).then((res) => {
    if (res.data.code == 200) {
      groupActiveData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};
const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  searchForm.value.pageSize = val;
  getGroupActive();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  searchForm.value.pageNum = val;
  getGroupActive();
};
onMounted(() => {
  getGroupActive();
});
</script>

<style scoped></style>
